﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.ETL
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class HttpDownloadInstruction:InstructionBase
    {
        #region props
        private string _RootUrl;
        /// <summary></summary>
        public string RootUrl
        {
            get
            {
                return this._RootUrl;
            }
            set
            {
                this._RootUrl = value;
            }
        }

        private string _DownloadLinkPattern;
        /// <summary></summary>
        public string DownloadLinkPattern
        {
            get
            {
                return this._DownloadLinkPattern;
            }
            set
            {
                this._DownloadLinkPattern = value;
            }
        }

        private string _DownloadToFolder;
        /// <summary></summary>
        public string DownloadToFolder
        {
            get
            {
                return this._DownloadToFolder;
            }
            set
            {
                this._DownloadToFolder = value;
            }
        }
        #endregion

        #region ctor
        /// <summary>
        /// 
        /// </summary>
        public HttpDownloadInstruction():base(Guid.Empty,Guid.Empty)
        {
            this._RootUrl = string.Empty;
            this._DownloadLinkPattern = string.Empty;
            this._DownloadToFolder = string.Empty;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="componentID"></param>
        /// <param name="jobTicketID"></param>
        /// <param name="rootUrl"></param>
        /// <param name="linkPattern"></param>
        /// <param name="folder"></param>
        public HttpDownloadInstruction(Guid componentID, Guid jobTicketID,
            string rootUrl, string linkPattern, string folder) : base(componentID, jobTicketID)
        {
            this._RootUrl = rootUrl;
            this._DownloadLinkPattern = linkPattern;
            this._DownloadToFolder = folder;
        }
        #endregion

        #region Overrides of InstructionBase

        /// <summary>
        /// serialize settings to xml
        /// the function below is mainly for storing into database as serialized form instead of string
        /// </summary>
        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Instruction");
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ComponentID", this.OwnerComponentID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SubmitTime", this.SubmitTime.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "StartTime", this.StartTime.ToString());

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "RootUrl",this.RootUrl);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "LinkPattern", this.DownloadLinkPattern);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TgtFolder", this.DownloadToFolder);

            return xNode;
        }

        /// <summary>
        /// instantiated from xml
        /// "&lt;Instructions&gt;&lt;Instruction&gt;&lt;/Instruction&gt;&lt;/Instruction&gt;", must
        /// instantiate from first child of root node
        /// </summary>
        public override InstructionBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                HttpDownloadInstruction instruction = new HttpDownloadInstruction();
                instruction.OwnerComponentID = new Guid(xNode.Attributes.GetNamedItem("ComponentID").Value);
                instruction.OwnerWorkerInstanceID = new Guid(xNode.Attributes.GetNamedItem("WorkerID").Value);
                instruction.SubmitTime = DateTime.Parse(xNode.Attributes.GetNamedItem("SubmitTime").Value);
                instruction.StartTime = DateTime.Parse(xNode.Attributes.GetNamedItem("StartTime").Value);

                instruction.RootUrl = XmlDataUtil.GetAttributeValue(xNode, "RootUrl", "");
                instruction.DownloadLinkPattern = XmlDataUtil.GetAttributeValue(xNode, "LinkPattern", "");
                instruction.DownloadToFolder = XmlDataUtil.GetAttributeValue(xNode, "TgtFolder", "");
                
                return instruction;
            }
            else
            {
                throw new Exception("Unable to instantiate instruction: invalid type specified in xml node");
            }
        }

        #endregion
    }
}
